/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.is.city.layermanager;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
import java.util.prefs.Preferences;



/**
 *
 * @author oleg
 */
public class SettingsController {

    Preferences prefs=null;

    public SettingsController() {
        initSettings();
    }
    
    private void initSettings() {
        
        try {
            FileInputStream is = new FileInputStream(this.getClass().getName());
            Preferences.importPreferences(is);
            is.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(LayerPanel.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Файл настроек не существует, но будет создан.");
        } catch (IOException ex) {
            Logger.getLogger(LayerPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidPreferencesFormatException ex) {
            Logger.getLogger(LayerPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        prefs = Preferences.userRoot().node(this.getClass().getName());
        String address = "Test1";
        System.out.println(prefs.get(address, "Hello world"));

        prefs.put(address, "Hello Mosco");
        System.out.println(prefs.get(address, "no value &"));

/*
        prefs.put("jdbc_url", "jdbc:postgresql://localhost/gis");
        prefs.put("jdbc_user", "oleg");
        prefs.put("jdbc_password", "");
        prefs.put("jdbc_ssl", "false");
*/
        save();
    }

    public void save(){
        FileOutputStream outputStream=null;
        try {
            outputStream = new FileOutputStream(this.getClass().getName());
            prefs.exportSubtree(outputStream);
            outputStream.close();
        } catch (BackingStoreException ex) {
            Logger.getLogger(LayerPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(LayerPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
